package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.enums.BasicEnum;
import com.zzyl.enums.ReservationStatus;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.AppointmentTimesVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CustomerReservationServiceimpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    /**
     * 新增预约
     *
     * @param reservationDto
     */
    @Override
    public void add(ReservationDto reservationDto) {

        Long userId = UserThreadLocal.getUserId();

        // 取消次数大于3次的也不能预约
        int cancelledReservationCount = Integer.parseInt((getCancelledReservationCount(userId)));

        // 如果已经预约的次数超过3次，则不需要再进行预约
        if (cancelledReservationCount >= 3) {
            throw new BaseException(BasicEnum.RESERVATION_CANCEL_COUNT_UPPER_LIMIT);
        }

        // 否则，允许添加预约
        Reservation reservation = new Reservation();
        BeanUtils.copyProperties(reservationDto, reservation);
        reservation.setStatus(ReservationStatus.PENDING.getOrdinal());
        reservation.setCreateBy(userId);

        customerReservationMapper.add(reservation);

    }

    /**
     * 分页查询预约
     *
     * @param pageNum
     * @param pageSize
     * @param status
     */
    @Override
    public PageResponse<ReservationVo> PaginationQuery(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<ReservationVo> page = customerReservationMapper.PaginationQuery(status);
        return PageResponse.of(page, ReservationVo.class);
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void cancelReservation(Long id) {
        customerReservationMapper.cancelReservation(id);
    }


    /**
     * 查询取消预约数量
     *
     * @return
     */
    @Override
    public String getCancelledReservationCount(Long userId) {
        return customerReservationMapper.countCancelledReservationsWithinTimeRange(LocalDateTime.now().withHour(0).withMinute(0).withSecond(0),
                LocalDateTime.now().withHour(23).withMinute(59).withSecond(59), userId);


    }




    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @Override
    public List<Map<String, Object>> countByTime(Long time) {
        LocalDateTime startTime = LocalDateTimeUtil.of(time);
        // 设置默认结束时间为开始时间 + 1 小时
        LocalDateTime endTime = startTime.plusHours(24);

        return customerReservationMapper.countByTime(startTime, endTime);
    }




}
